package com.qf.controller.refundordercontroller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qf.entity.refundorder.RefundOrder;
import com.qf.service.refundorderservice.RefundOrderService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * --- 今日无事,勾栏听曲 ---
 *
 * @author JinZhiHao
 * @date 2024/12/6
 * @desc
 */

@RestController
@RequestMapping("/refund")
public class RefundOrderController {


    @Autowired
    private RefundOrderService refundOrderService;

    /**
     * 模糊查询退款订单
     */
    @GetMapping("/list")
    public R list(RefundOrder refundOrder){
        PageHelper.startPage(refundOrder.getPageNum(),refundOrder.getPageSize());

        List<RefundOrder> list = refundOrderService.list(refundOrder);
        System.out.println("list = " + list);

        PageInfo<RefundOrder> pageInfo = new PageInfo(list);
        System.out.println("pageInfo = " + pageInfo);

        return R.ok(pageInfo);
    }

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws Exception {
        RefundOrder refundOrder = new RefundOrder();
        List<RefundOrder> list = refundOrderService.list(refundOrder);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("用户信息表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), RefundOrder.class).sheet("用户信息表").doWrite(list);
    }

    /**
     * 通过id删除退款订单
     */
    @GetMapping("/del")
    public R del( Integer id){
        int i =  refundOrderService.del(id);
        if(i > 0){
            return R.ok(20000);
        }
        return R.fail();
    }

    /**
     * 通过id更改审核状态
     */
    @PostMapping("/editStatus")
    public R editStatus(@RequestBody RefundOrder refundOrder) {
        int i = refundOrderService.editStatus(refundOrder);
        if (i > 0) {
            return R.ok();
        }
        return R.fail();
    }
}
